package com.ivideo.backend.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivideo.backend.base.utils.Result;
import com.ivideo.backend.base.utils.ResultCodeEnum;
import com.ivideo.backend.model.system.SysRole;

import com.ivideo.backend.model.vo.AssignRoleVo;
import com.ivideo.backend.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Project: ivideo-parent
 * @Name: SysRoleController
 * @Author: Jack_Li
 * @Date: 2024/5/20 14:06
 */

@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation(value = "测试接口")
    @GetMapping("/test")
    public Result test() {
//        return Result.success(sysRoleService.list());
        return Result.success(null);
    }

    /**
     * 角色列表
     * @param page
     * @param size
     * @param keyword
     * @return
     */
    @ApiOperation(value = "角色列表")
    @GetMapping(value = "/list/{page}/{size}")
    public Result<Page<SysRole>> list(
            @PathVariable Long page,
            @PathVariable Long size,
            @RequestParam(required = false) String keyword) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if(keyword != null) {
            queryWrapper.like(SysRole::getRoleName, keyword);
        }
        Page<SysRole> rolePage = sysRoleService.page(new Page<>(page, size), queryWrapper);
        return Result.success(rolePage);
    }

    /**
     * 添加或修改用户
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "添加或修改角色")
    @PostMapping(value = "/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysRole sysRole) {
        log.info("saveOrUpdate User：{}", sysRole);
        if(sysRole == null || sysRole.getRoleName() == null || sysRole.getRoleCode() == null) {
            return Result.error(ResultCodeEnum.NULL.getCode(), "角色名或编码不能为空");
        }
        if (sysRole.getId() == null) {
            LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysRole::getRoleName, sysRole.getRoleName());
            SysRole role = sysRoleService.getOne(queryWrapper);
            if (role != null) {
                return Result.error(ResultCodeEnum.USER_OR_PASSWORD_ERROR.getCode(), "角色名已存在");
            }
        }
        sysRoleService.saveOrUpdate(sysRole);
        return Result.success(null);
    }

    /**
     * 用户删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "角色删除")
    @DeleteMapping(value = "/remove")
    public Result remove(@RequestBody List<Long> ids) {
        log.info("remove Roles：{}", ids);
        sysRoleService.removeByIds(ids);
        return Result.success(null);
    }

    /**
     * 根据用户获取角色数据
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRoleByUserId(userId);
        return Result.success(roleMap);
    }

    /**
     * 根据用户分配角色
     * @param assignRoleVo
     * @return
     */
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleVo assignRoleVo) {
        sysRoleService.doAssign(assignRoleVo);
        return Result.success(null);
    }
}
